﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ciripescu_WebRole.Entities;
using Microsoft.Samples.ServiceHosting.StorageClient;
using System.Data.Services.Client;

namespace Ciripescu_WebRole.BL
{
    public class InviteManager
    {
        #region Public Methods

        public InviteManager()
        {
            m_svcContext             = new MyServiceContext();
            m_svcContext.RetryPolicy = RetryPolicies.RetryN(3, TimeSpan.FromSeconds(1));
        }

        public IEnumerable<Invite> DoQuery(IQueryable<Invite> q)
        {
            TableStorageDataServiceQuery<Invite> query = new TableStorageDataServiceQuery<Invite>(q as DataServiceQuery<Invite>);
            IEnumerable<Invite> queryResults           = query.ExecuteAllWithRetries();
            
            return queryResults;
        }

        public Invite GetItem(string email)
        {
            try
            {
                return (DoQuery(from c in m_svcContext.Invite where c.PartitionKey == email select c)).Single();
            }
            catch (Exception e)
            {
                Utils.WriteLog(EventLogName.Error, e.ToString());
                return null;
            }
        }

        

        public IEnumerable<Invite> GetAllItems()
        {
            var results = from c in m_svcContext.Invite select c;
            return DoQuery(results);
        }

        public void Delete(Invite itemToDelete)
        {
            m_svcContext.DeleteObject(itemToDelete);
            m_svcContext.SaveChanges();
        }

        public void DeleteWithoutQ(Invite itemToDelete)
        {
            m_svcContext.AttachTo(m_svcContext.InviteTableName, itemToDelete, "*");
            Delete(itemToDelete);
        }

        /// <summary>
        /// Updates an existing item from the tables
        /// </summary>
        /// <param name="item"></param>
        public void Save(Invite item)
        {
            m_svcContext.UpdateObject(item);
            m_svcContext.SaveChanges();
        }

        /// <summary>
        /// Updates an existing item, but did not commit it to the storage.
        /// </summary>
        /// <param name="item"></param>
        public void SaveWithNoCommit(Invite item)
        {
            m_svcContext.UpdateObject(item);
        }

        /// <summary>
        /// Inserts a new item in the storage.
        /// </summary>
        /// <param name="newItem"></param>
        public void Insert(Invite newItem)
        {
            m_svcContext.AddObject(m_svcContext.InviteTableName, newItem);
            m_svcContext.SaveChanges();
        }

        /// <summary>
        /// Inserts a new item, but did not commit it to the storage.
        /// </summary>
        /// <param name="newItem"></param>
        public void InsertWithNoCommit(Invite newItem)
        {
            m_svcContext.AddObject(m_svcContext.InviteTableName, newItem);
        }

        public void Commit()
        {
            m_svcContext.SaveChanges();
        }

        #endregion 

        #region Private Methods
        private MyServiceContext m_svcContext = null;
        #endregion
    }
}
